﻿using ET_DbCommon.DAL;
using ET_DbCommon.Model;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace ET.ScoreTeacherClient.Views
{
    /// <summary>
    /// Paper.xaml 的交互逻辑
    /// </summary>
    public partial class Paper : Window
    {
        public Paper(PaperModel _paperModel)
        {
            InitializeComponent();
            paperModel = _paperModel;
            this.DataContext = this;
        }
        public PaperModel paperModel { get; set; }
        public ObservableCollection<LevelModel> levelModels { get; set; } = new ObservableCollection<LevelModel>();
        List<Paper_TestQuestionModel>  paperTests { get; set; } = new List<Paper_TestQuestionModel>();
        
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            levelModels.Add(new LevelModel { ID = 1, Level = "1" });
            levelModels.Add(new LevelModel { ID = 2, Level = "2" });
            levelModels.Add(new LevelModel { ID = 3, Level = "3" });
            levelModels.Add(new LevelModel { ID = 4, Level = "4" });
            levelModels.Add(new LevelModel { ID = 5, Level = "5" });
            this.combolevel.ItemsSource = levelModels;
            paperTests = Paper_TestQuestionDAL.GetAll();
            cmbTestPaper.ItemsSource = paperTests;
            if (!string.IsNullOrEmpty(paperModel.TesquestiontList))
            {
                StringBuilder sb = new StringBuilder();
                foreach (var item in paperModel.TesquestiontList.Split(','))
                {
                    if (int.TryParse(item, out int num))
                    {
                        sb.Append(paperTests.FirstOrDefault(t => t.Id == num).TestName).Append(",");
                    }
                }
                paperModel.TesquestiontList = sb.ToString().TrimEnd(',');
                cmbTestPaper.Text = sb.ToString().TrimEnd(',');
            }
            this.DataContext = this;
        }
        private void Window_Closed(object sender, EventArgs e)
        {
          //  tokenSource.Cancel();//取消筛选的Task
        }
        private void BtnSave_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(paperModel.Describes))
            {
                MessageBox.Show("请输入描述！");
                return;
            }
            if (!string.IsNullOrEmpty(cmbTestPaper.Text))
            {
                StringBuilder sb = new StringBuilder();
                foreach (var item in cmbTestPaper.Text.Split(','))
                {
                    sb.Append(paperTests.FirstOrDefault(t => t.TestName == item).Id.ToString()).Append(",");
                }
                paperModel.TesquestiontList = sb.ToString().TrimEnd(',');
            }
            if (paperModel.Id == 0)
            {
                var newId = PaperDAL.Insert(paperModel);
                paperModel.Id = newId;
                this.DialogResult = true;
            }
            else
            {
                paperModel.UpdateTime = DateTime.Now;
                if (PaperDAL.Update(paperModel) > 0)
                {
                    this.DialogResult = true;
                }
            }
                
        }

        private void BtnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
    public class LevelModel
    {
        public int ID { get; set; }
        public string  Level { get; set; }
    }
}
